import { A } from './0_common';

// type Awaited<T> = T extends PromiseLike<infer U> ? U : T;

// type _Awaited<T> = T extends Promise<infer R> ? _Awaited<R> : never;

/**
 * Promise
 */
interface Promise<T> {
  then<TResult1 = T, TResult2 = never>(
    onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
    onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
  ): Promise<TResult1 | TResult2>;
  catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;
}
interface PromiseLike<T> {
  then<TResult1 = T, TResult2 = never>(
    onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
    onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
  ): PromiseLike<TResult1 | TResult2>;
}
/**

type Awaited<T> = T extends null | undefined
  ? T
  : T extends object & { then(onfulfilled: infer F, ...args: infer _): any }
  ? F extends (value: infer V, ...args: infer _) => any
    ? Awaited<V>
    : never
  : T;

 */

type _Awaited<T> = T extends null | undefined //判断是否是null或者undefined
  ? T
  : T extends object & {
      then(onfulfilled: infer F, ...args: infer _ ):any; 
    } //判断是否是promise
    ? F extends (value: infer V, ...args: infer _) => any //promise嵌套
      ? _Awaited<V> 
      : never 
    : T;
